package com.tb.education.handler;

import cn.hutool.core.bean.BeanUtil;
import com.tb.education.common.SessionContext;
import com.tb.education.common.intf.AbsBaseHandler;
import com.tb.education.common.packets.BaseBody;
import com.tb.education.common.packets.DevicePacket;
import com.tb.education.common.packets.Head;
import com.tb.education.common.packets.Msg;
import com.tb.education.common.packets.response.DeviceLoginResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;

/**
 * @author wangze
 * @date 2020-07-12 10:54.
 **/
public class DeviceLoginHandler extends AbsBaseHandler {

	private static Logger log = LoggerFactory.getLogger(DeviceLoginHandler.class);

	@Override
	public Object handler(DevicePacket packet, Head head, String requestBody, ChannelContext channelContext) throws Exception {
		//TODO 做一些登录相关操作
		log.info("登录成功: 设备号" + head.getDeviceNo());
		Tio.bindUser(channelContext, head.getDeviceNo());
		SessionContext sessionContext = (SessionContext) channelContext.getAttribute();
		sessionContext.setDeviceNo(head.getDeviceNo());
		sessionContext.setIccId(head.getIccId());
		DeviceLoginResponseBody responseBody = new DeviceLoginResponseBody();
		DevicePacket devicePacket = new DevicePacket();
		devicePacket.setBody(Msg.getServerToClientByte(head, responseBody));
		Tio.send(channelContext, devicePacket);
		return null;
	}
}
